package com.yunxi.poc.starbucks.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunxi.poc.starbucks.common.BaseController;
import com.yunxi.poc.starbucks.entity.TOrderNew;
import com.yunxi.poc.starbucks.service.ITOrderNewService;
import com.yunxi.poc.starbucks.vo.BuyVO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shiyuan.xlc
 * @since 2021-08-18
 */
@Controller
@RequestMapping("/starbucks/order_new")
public class TOrderNewController extends BaseController {

    @Resource
    ITOrderNewService orderNewService;

    @ResponseBody
    @RequestMapping(value = {""}, method = RequestMethod.POST)
    public ResponseEntity pageOrderNew(@RequestBody BuyVO buyerInfo, Page pageInfo) {

        QueryWrapper<TOrderNew> queryWrapper = new QueryWrapper<>();

        if (buyerInfo.getOrderIds() == null || buyerInfo.getOrderIds().length == 0) {

            if (ObjectUtils.isEmpty(buyerInfo.getStart()) || ObjectUtils.isEmpty(buyerInfo.getEnd())) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("0");
            }
            if (buyerInfo.getStoreIds().length > 0) queryWrapper.in("store_id", buyerInfo.getStoreIds());
            if (StringUtils.hasLength(buyerInfo.getStatus())) queryWrapper.eq("cal_status", buyerInfo.getStatus());
            queryWrapper.ge("create_date", buyerInfo.getStart());
            queryWrapper.lt("create_date", buyerInfo.getEnd());

        } else {
            queryWrapper.in("id", buyerInfo.getOrderIds());
        }

        queryWrapper.select(TOrderNew.class, i -> !i.getProperty().startsWith("trace"));
        Page<TOrderNew> pageData = orderNewService.page(pageInfo, queryWrapper);
        return ResponseEntity.status(HttpStatus.OK).body(pageData);
    }
}
